package hopital.orm;

import hopital.sql.SqlDatabase;
import hopital.sql.SqlObject;
import hopital.sql.SqlQuery;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Facture extends SqlObject{
    
    public List<Object> getDetail() throws Exception {
        return SqlDatabase.getDatabase().retrive(new DetailFacture(this._id));
    }

    public int getPatient() {
        return _patient;
    }
    
    private int _comptable,_patient ;
    private double _prix;
    public int getComptable() {
        return _comptable;
    }

    public void setComptable(int _comptable) {
        this._comptable = _comptable;
    }
    private String _date,_etat;

    public double getPrix() {
        return _prix;
    }

    public void setPrix(double _prix) {
        this._prix = _prix;
    }



    public String getEtat() {
        return _etat;
    }

    public void setEtat(String _etat) {
        this._etat = _etat;
    }
    
    public int getDocteur() {
        return _comptable;
    }


    public void setPatient(int _patient) {
        this._patient = _patient;
    }

    public String getDate() {
        return _date;
    }

    public void setDate(String _date) {
        this._date = _date;
    }

    public Facture(int _comptable, int _patient, String _date) {
        this._comptable = _comptable;
        this._patient = _patient;
        this._date = _date;
    }



    

    public Facture() {
    }

    
    
    private Facture(ResultSet _rs) throws SQLException {
       this(_rs.getInt("c_comptable"), _rs.getInt("c_patient"), _rs.getString("c_date"));
       this._id=_rs.getInt("c_id");
       this._prix=_rs.getInt("c_prix");
    }
    
    @Override
    public SqlQuery insert() {
        return new SqlQuery("INSERT INTO t_factures (c_comptable,c_patient,c_date) VALUES(?,?,?);",_comptable,_patient,_date);
    }

    @Override
    public SqlQuery update() {
        return new SqlQuery("UPDATE t_factures SET c_etat=? WHERE c_id=?;",_id,_etat);
    }

    @Override
    public SqlQuery delete() {
        return new SqlQuery("DELETE FROM t_factures WHERE c_id=?;", _id);
    }

    @Override
    public SqlQuery select() {
        if(_id!=-1){
            return new SqlQuery("SELECT t_factures.*,sum(c_prix) AS c_prix FROM t_factures JOIN t_detail_factures ON t_factures.c_id=t_detail_factures.c_facture  WHERE t_factures.c_id=? GROUP BY t_factures.c_id", _id);
        }else if(_patient!=0){
            return new SqlQuery("SELECT t_factures.*,sum(c_prix) AS c_prix FROM t_factures JOIN t_detail_factures ON t_factures.c_id=t_detail_factures.c_facture  WHERE c_patient=? GROUP BY t_factures.c_id;", _patient);
        }else{
            return new SqlQuery("SELECT t_factures.*,sum(c_prix) AS c_prix FROM t_factures JOIN t_detail_factures ON t_factures.c_id=t_detail_factures.c_facture GROUP BY t_factures.c_id;");
        }
    }

    @Override
    public List<Object> getTable(ResultSet _rs) throws Exception {
        List<Object> list=new ArrayList<>();
        while (_rs.next()) {            
            list.add(new Facture(_rs));
        }
        return list;
    }

    @Override
    public boolean save(SqlDatabase _db) throws Exception {
        boolean b= super.save(_db); 
        if(b){
           ResultSet _rs=SqlDatabase.getDatabase().getKey(); 
            if (_rs.next()) {
                this._id=_rs.getInt(1);
            }
        }
        return b;
    }
    
    
}
